import os

from django.shortcuts import render, redirect

# Create your views here.
# your_app_name/views.py

from django.shortcuts import render
from django.http import HttpResponse
import pandas as pd
from django.http import JsonResponse
from django.conf import settings

from mainapp.models import ProductLotRel


def index(request):
    return redirect("product_lot_rel_list")


def process_config(request):
    """返回excel文件的配置结果"""

    device_names_config = [
        {"process_number": 101, "process_name": "频率及△F测试", "device_name": "MT-01"},
        {"process_number": 102, "process_name": "直角度测试", "device_name": "CA-13"},
        {"process_number": 103, "process_name": "尺寸测试", "device_name": "DS-01"},
    ]

    filepath = settings.BASE_DIR / f"datas/database/database.xlsx"
    if not os.path.isfile(filepath):
        return JsonResponse({"code": 2, "message": f"错误，{filepath}文件找不到"})

    products = ProductLotRel.objects.all().values()
    products_list = list(products)
    result = {
        "device_config": device_names_config,
        "process_database": products_list
    }
    return JsonResponse(result)
